package cluster

import (
	"context"
	"gitee/yinlai/kms-backend/config"
	"gitee/yinlai/kms-backend/utils/logs"
	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"net/http"
)

func Delete(r *gin.Context) {
	logs.Debug(nil, "删除集群")
	// 1.接受删除集群的id
	clusterId := r.Query("id")
	returnData := config.ReturnData{}

	err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Delete(context.TODO(), clusterId, metav1.DeleteOptions{})

	if err != nil {
		logs.Error(map[string]interface{}{"msg": "删除集群失败", "clusterId": clusterId}, "删除失败")
		// 说明删除失败
		msg := "集群删除失败：" + err.Error()
		returnData.Status = 400
		returnData.Message = msg
	} else {
		logs.Warning(map[string]interface{}{"msg": "删除集群成功", "clusterId": clusterId}, "删除集群成功")
		// 说明删除成功
		returnData.Status = 200
		returnData.Message = "删除成功"
		// 集群删除完成后更新 config.ClusterKubeconfig
		delete(config.ClusterKubeconfig, clusterId)
		//fmt.Println("当前集群配置：", config.ClusterKubeconfig)
	}

	r.JSON(http.StatusOK, returnData)
	return
}
